import com.mochimedia.ui.*;

class com.mochimedia.ui.ComboBox extends Cell {

    private var _type:String = "combobox";
    
    private var _textLabel:String = "";
    private var _choices:Array;
    private var _promptText:String = "";
    
    private var _open:Boolean = false;
    
    private var _field:FormField;
    private var _fieldbutton:BButton;
    private var _button:BButton;
    private var _dropdown:Cell;
    
    private var _selectionIndex:Number = -1;
    private var _activeChoice:BButton;
    
    private var _homeDepth:Number = 1;
    
    public function get value ():String { return _field.value; }
    
    //
    //
    //
    function ComboBox (container:MovieClip, textLabel:String, choices:Array, selectionIndex:Number, promptText:String, width:Number, position:Position, style:Style) {
        
        init (container, textLabel, choices, selectionIndex, promptText, width, position, style);
        
        if (_container != null) create();
        
    }
    
    //
    //
    private function init (container:MovieClip, textLabel:String, choices:Array, selectionIndex:Number, promptText:String, width:Number, position:Position, style:Style):Void {
        
        super.init(container, NaN, NaN, false, false, 0, position, style);

        _textLabel = (textLabel != undefined && textLabel.length > 0) ? textLabel : _textLabel;
        _choices = choices.concat();
        _selectionIndex = (!isNaN(selectionIndex)) ? selectionIndex : -1;
        _promptText = (promptText != undefined && promptText.length > 0) ? promptText : _promptText;
        _width = (!isNaN(width)) ? width : 224;
        
    }
    
    //
    //
    public function create ():Void {
        
        super.create();
        
        _field = FormField(addChild(new FormField(null, _promptText, _width - 24, NaN, false, new Position( { placement: Position.PLACEMENT_FLOAT, margins: 0 } ), _style)));
        _field.clear = false;
		_field.tabEnabled = false;
		_field.editable = false;
        
        var buttonStyle:Style = _style.clone();
        buttonStyle.round = 0;
        buttonStyle.borderWidth = 2;
        
        _button = BButton(addChild(new BButton(null, Create.ICON_ARROW_DOWN, null, _field.height, _field.height, false, false, false, new Position( { placement: Position.PLACEMENT_FLOAT, margins: 0 } ), buttonStyle)));
        
        var dropdownStyle:Style = _style.clone();
        dropdownStyle.borderWidth = 2;
        dropdownStyle.borderColor = ColorTools.getTintedColor(_style.borderColor, _style.backgroundColor, 0.5);

        _dropdown = Cell(addChild(new Cell(null, _width - 24, 100, true, true, 0, null, dropdownStyle)));
        _dropdown.maskContent = _dropdown.trapMouse = _dropdown.collapse = true;
        _dropdown.hide();
        
        var choicesStyle:Style = _style.clone();
        choicesStyle.background = choicesStyle.border = choicesStyle.gradient = false;
        choicesStyle.unselectedTextColor = 0x333333;
        choicesStyle.inverseTextColor = 0x999999;
        choicesStyle.round = 0;
        choicesStyle.padding = 1;
        
        var b:BButton;
        
        for (var i:Number = 0; i < _choices.length; i++) {
            
            b = BButton(_dropdown.addChild(new BButton(null, _choices[i], Position.ALIGN_LEFT, _width - 24, NaN, false, true, false, new Position( { margins: 0 }), choicesStyle)));
            b.addEventListener(EVENT_CLICK, this, onChoice);
            
        }
        
        _fieldbutton = BButton(addChild(new BButton(null, "", null, _width - 24, 24, false, false, false, new Position( { placement: Position.PLACEMENT_ABSOLUTE, top: 0, left: 0 } ), choicesStyle)));
        
        _button.addEventListener(EVENT_CLICK, this, toggle);
        _fieldbutton.addEventListener(EVENT_CLICK, this, toggle);
        
        addEventListener(EVENT_BLUR, _dropdown, _dropdown.hide);
        
        _height = _field.height;
        
        if (_selectionIndex >= 0) {
            
            BButton(_dropdown.childNodes[_selectionIndex]).dispatchEvent(EVENT_CLICK);
            toggle();
            
        }
        
    }
    
    //
    //
    public function onChoice (e:Event):Void {
        
        for (var i:Number = 0; i < _dropdown.childNodes.length; i++) if (_dropdown.childNodes[i] != e.target) BButton(_dropdown.childNodes[i]).deselect();
        
        _activeChoice = BButton(e.target);

        _activeChoice.select();
        
        _field.text = _activeChoice.textLabel;

        dispatchEvent(EVENT_CLICK);
        
        toggle();
        
        
    }
    
    //
    //
    public function toggle (e:Event):Void {
        
        _dropdown.toggle();
        
        if (_dropdown.visible) {
            
            _homeDepth = _mc.getDepth();
            _mc.swapDepths(parentCell.childNodes.length - 1);
            _field.onFocus();
            
            if (e == undefined) dispatchEvent(EVENT_FOCUS);
  
        } else {
            
            _mc.swapDepths(_homeDepth);
            _field.onBlur();

            if (e == undefined) dispatchEvent(EVENT_BLUR);
            
        }
        
    }
    
    //
    //
    public function onBlur (e:Event):Void {
    
        if (_dropdown.visible) toggle(e);

    }
    
    
}
